package com.leetcode.August;

import javax.swing.tree.TreeNode;
import java.util.ArrayList;
import java.util.Collections;

/**
 * @Description: 二叉搜索树中第K小的元素  二叉搜索树的特性
 * @Author: Mr.Fxy
 * @CreateTime: 2024-08-01 09:11
 */
public class kelementTree {
    int cnt = 0, ans = 0;
    public int kthSmallest(TreeNode root, int k) {
        order(root,k);
        return ans;
    }
    public void order(TreeNode root, int k)
    {
        if(root == null)
            return;
        order(root.left,k);
        cnt++;
        if(cnt == k)
        {
            ans = root.val;
            return;
        }
        order(root.right,k);
    }
}
